package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Movie;
import com.itwn.entity.UrlPermission;
import com.itwn.entity.actor;
import com.itwn.result.ResponseResult;
import com.itwn.service.ActorService;
import com.itwn.service.MovieService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin
@Api
@RequestMapping("Actor")
public class ActorController {

    @Autowired
    private ActorService actorService;


    @GetMapping("/actorList")
    public PageInfo<actor> findWithPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                        String actorName){

        PageInfo<actor> pageInfo = actorService.findActorPage(pageNum, pageSize,actorName);
        return pageInfo;
    }


    @Autowired
    CinemaController cinemaController;

    @PostMapping("/actorAdd")
    public ResponseResult<actor> add(@RequestParam(value = "fileImg",required = false) MultipartFile fileImg, actor actor) throws IOException {
        ResponseResult<actor> result = new ResponseResult<>();


        String uploadImg = cinemaController.uploadImg(fileImg);
        actor.setActorImg(uploadImg);
        int i= actorService.addActor(actor);
        if (i == 1){
            result.setCode(200);
            result.setData(actor);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

    @PostMapping("/updateActor")
    public ResponseResult<actor> updatePer(@RequestParam(value = "fileImg",required = false)MultipartFile fileImg, actor actor) throws IOException {


        ResponseResult<actor> result = new ResponseResult<>();

        String uploadImg = cinemaController.uploadImg(fileImg);
        actor.setActorImg(uploadImg);
        int i= actorService.updateMovie(actor);
        if (i == 1){
            result.setCode(200);
            result.setData(actor);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

    @PostMapping("/deleteActor")
    public ResponseResult<UrlPermission> removePermission(@RequestParam(value = "id") int id){
        ResponseResult<UrlPermission> result = new ResponseResult<>();
        int i= actorService.removeById(id);
        if (i == 1){
            result.setCode(200);
            return result ;
        }else {
            result.setCode(101);
            return result;
        }
    }

}
